﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.PhongCat;
using QuanLyKho.PhongGiaCong;

namespace QuanLyKho.PhongHoanThien
{
    public partial class SanPhamHoanThien : UserControl
    {
        public SanPhamHoanThien()
        {
            InitializeComponent();
            LoadData();
            SetPropertiesGrid();
            SetPropertiesColumns();
        }

        public void LoadData()
        {
            DataTable kcs = DataAll.SelectAll("KCS inner join LenhSanXuat on KCS.SoChungTu = LenhSanXuat.SoChungTu inner join PhongMau_SanPham on KCS.IdSanPham = PhongMau_SanPham.IdSanPham",
                "*,LenhSanXuat.ID as 'LsxID',KCS.ID as 'KCSID',KCS.TrangThai as 'TrangThai1'",
                "WHERE KCS.TrangThai = " + Convert.ToInt32(TrangThai.KCS_DaDuyet));
            gridKCS.DataSource = kcs;
            DataTable data = DataAll.SelectAll("HoanThien inner join PhongMau_SanPham on HoanThien.IdSanPham = PhongMau_SanPham.IdSanPham",
                    "PhongMau_SanPham.IdSanPham,PhongMau_SanPham.[MaHang] as MaSanPham,PhongMau_SanPham.[LoaiHang],PhongMau_SanPham.[LanSanXuat],PhongMau_SanPham.[LenhSanXuat],HoanThien.[ID],HoanThien.[IdSanPham],HoanThien.[NgayLapLenh],HoanThien.[NguoiCapNhatLenh],HoanThien.[SoLuong],HoanThien.[NgayGuiLenh],HoanThien.[NgaySuaLenh],HoanThien.[TrangThai],HoanThien.[NgayChungTu],HoanThien.[NgayHoachToan],HoanThien.[SoChungTu],HoanThien.[GhiChu],HoanThien.[KhoDen],HoanThien.[KhoDi],HoanThien.[SXTuNgay],HoanThien.[SXDenNgay]",
                "WHERE HoanThien.TrangThai = " + Convert.ToInt32(TrangThai.HoanThien_TaoMoi));
            gridHoanThien.DataSource = data;
        }

        public void SetPropertiesGrid()
        {
            gvHoanThien.BestFitColumns();
            gvHoanThien.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            gvHoanThien.Appearance.SelectedRow.BackColor = Color.Empty;
            gvHoanThien.Appearance.SelectedRow.Options.UseBackColor = true;
            gvHoanThien.OptionsView.ShowGroupPanel = false;
            //gvMayKhoan.OptionsView.ColumnAutoWidth = false;
            gvHoanThien.OptionsView.ShowFooter = true;
            gvHoanThien.OptionsFind.AlwaysVisible = true;
            gvHoanThien.Columns["ID"].Visible = false;

            gvKCS.BestFitColumns();
            gvKCS.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvKCS.OptionsBehavior.Editable = false;
            gvKCS.OptionsSelection.MultiSelect = true;
            gvKCS.OptionsView.ShowGroupPanel = false;
            gvKCS.OptionsView.ShowFooter = true;
            gvKCS.OptionsFind.AlwaysVisible = true;
            gvKCS.Columns["LsxID"].Visible = false;
            gvKCS.Columns["KCSID"].Visible = false;

        }

        public void SetPropertiesColumns()
        {
            gcGuiThanhPham.Width = 70;
            gcGuiThanhPham.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcMaSanPham.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayLapLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNguoiLapLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSoLuong.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgaySuaLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcTrangThai.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayChungTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayHoachToan.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSoChungTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcGhiChu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            //gcS.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcM.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcL.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcXL.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcTongCan.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcDMTT.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcSLCan.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
        }

        private void gvKCS_DoubleClick(object sender, EventArgs e)
        {
            if (gvKCS.RowCount > 0)
            {
                var dataview = (DataView)gvKCS.DataSource;
                var data = DataAll.SelectAll("HanhTrinh", "*", " WHERE SoChungTu='" + dataview[gvKCS.FocusedRowHandle]["SoChungTu"]
                    + "' AND TrangThai=" + Convert.ToInt32(TrangThai.KCS_HoanThienDaNhan));
                if (data.Rows.Count <= 0)
                {
                    DataAll.InsertAll("HanhTrinh", dataview[gvKCS.FocusedRowHandle]["MaHang"]
                                + "," + Convert.ToInt32(PhongBan.PhongHoanThien)
                                + "," + dataview[gvKCS.FocusedRowHandle]["SoChungTu"]
                                + "," + Convert.ToInt32(TrangThai.KCS_HoanThienDaNhan)
                                + "," + Main.UserID
                                + "," + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff")
                                + "," + dataview[gvKCS.FocusedRowHandle]["SoLuong"]
                                + "," + Convert.ToDateTime(dataview[gvKCS.FocusedRowHandle]["TuNgay"].ToString()).ToString("MM/dd/yyyy")
                                + "," + Convert.ToDateTime(dataview[gvKCS.FocusedRowHandle]["TuNgay"].ToString()).ToString("MM/dd/yyyy"));
                }
                CapNhatHoanThien.IdLenh = gvKCS.Columns["LsxID"].View.GetRowCellDisplayText(gvKCS.FocusedRowHandle, "LsxID");
                CapNhatHoanThien.IdKCS = gvKCS.Columns["KCSID"].View.GetRowCellDisplayText(gvKCS.FocusedRowHandle, "KCSID");
                var lss = new CapNhatHoanThien();
                lss.ShowDialog();
                LoadData();
                CapNhatHoanThien.IdLenh = "0";
            }
        }

        private void gvKCS_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai1")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
        }

        private void gvHoanThien_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
        }

        private void gvHoanThien_DoubleClick(object sender, EventArgs e)
        {
            if (gvHoanThien.FocusedRowHandle >= 0)
            {
                string a = gvHoanThien.GetRowCellValue(gvHoanThien.FocusedRowHandle, "TrangThai").ToString();
                string b = Convert.ToInt32(TrangThai.HoanThien_TaoMoi).ToString();
                if (a == b)
                {
                    if (gvHoanThien.FocusedRowHandle >= 0)
                    {
                        CapNhatHoanThien.MaID =
                            gvHoanThien.Columns["ID"].View.GetRowCellDisplayText(gvHoanThien.FocusedRowHandle, "ID");
                        var lss = new CapNhatHoanThien();
                        lss.ShowDialog();
                        LoadData();
                        CapNhatHoanThien.MaID = "0";
                    }
                }
            }
        }

        private void btGuiThanhPham_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvHoanThien.DataSource;
            string a = gvHoanThien.GetRowCellValue(gvHoanThien.FocusedRowHandle, "TrangThai").ToString();
            string b = Convert.ToInt32(TrangThai.HoanThien_TaoMoi).ToString();
            if (a == b)
            {
                DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn gửi.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    string message = DataAll.Update("HoanThien", "TrangThai =" + Convert.ToInt32(TrangThai.HoanThien_DaGui) + ",NgayGuiLenh ='" + DateTime.Now.ToString("MM/dd/yyyy") + "' WHERE ID =" + dataview[gvHoanThien.FocusedRowHandle]["ID"]);
                    message = DataAll.Update("PhongMau_SanPham", "TrangThai =" + Convert.ToInt32(TrangThai.HoanThien_DaGui) + " WHERE IdSanPham=" + dataview[gvHoanThien.FocusedRowHandle]["IdSanPham"]);
                    MessageBox.Show(message.Equals("success") ? "Gửi phòng thành phẩm thành công" : message, "Message",
                                        MessageBoxButtons.OK,
                                        message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                    DataAll.InsertAll("HanhTrinh", dataview[gvHoanThien.FocusedRowHandle]["MaSanPham"]
                            + "," + Convert.ToInt32(PhongBan.PhongHoanThien)
                            + "," + dataview[gvHoanThien.FocusedRowHandle]["SoChungTu"]
                            + "," + Convert.ToInt32(TrangThai.HoanThien_DaGui)
                            + "," + Main.UserID
                            + "," + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff")
                            + "," + dataview[gvHoanThien.FocusedRowHandle]["SoLuong"]
                            + "," + Convert.ToDateTime(dataview[gvHoanThien.FocusedRowHandle]["SXTuNgay"].ToString()).ToString("MM/dd/yyyy")
                            + "," + Convert.ToDateTime(dataview[gvHoanThien.FocusedRowHandle]["SXDenNgay"].ToString()).ToString("MM/dd/yyyy"));
                    
                    LoadData();
                }
            }
        }

        private void gvHoanThien_RowStyle(object sender, RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                GridView View = sender as GridView;
                string trangthai = View.GetRowCellDisplayText(e.RowHandle, View.Columns["TrangThai"]);
                if (trangthai == "Phòng hoàn thiện tạo mới")
                {
                    e.Appearance.BackColor = Color.Empty;
                }
                else
                {
                    e.Appearance.BackColor = Color.DarkSeaGreen;
                }
            }
        }

        private void gvKCS_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "SXDenNgay")
            {
                TimeSpan songay = Convert.ToDateTime(e.CellValue) - DateTime.Now;
                if (0 < songay.Days && songay.Days < 5)
                {
                    e.Appearance.BackColor = Color.PaleGoldenrod;
                }
                if (songay.Days < 0)
                {
                    e.Appearance.BackColor = Color.DarkGray;
                }
            }
        }

        private void btnHuy_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvHoanThien.DataSource;

            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn hủy ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                string message = DataAll.Update("KCS", "TrangThai = " + Convert.ToInt32(TrangThai.KCS_DaDuyet) + " WHERE SoChungTu ='" + dataview[gvHoanThien.FocusedRowHandle]["SoChungTu"] + "'");
                if (message.Equals("success"))
                {
                    DataAll.Delete("HoanThien", "ID =" + dataview[gvHoanThien.FocusedRowHandle]["ID"]);
                }
                MessageBox.Show(message.Equals("success") ? "Hủy thành công" : message, "Message",
                                   MessageBoxButtons.OK,
                                   message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
            }

            LoadData();
        }

        private void btnGuiTra_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvKCS.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn gửi trả.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                DataTable dtGiaCong = DataAll.SelectAll("GiaCong", "*", "WHERE SoChungTu = '" + dataview[gvKCS.FocusedRowHandle]["SoChungTu"] + "'");
                int trangthai = Convert.ToInt32(dtGiaCong.Rows[0]["TrangThai"].ToString()) == Convert.ToInt32(TrangThai.GiaCong_KCSDaDuyet) ? Convert.ToInt32(TrangThai.GiaCong_GuiDi) : Convert.ToInt32(TrangThai.MayKhoan_GuiDi);
                string message = DataAll.Update("GiaCong", "TrangThai = " + trangthai + " WHERE SoChungTu ='" + dataview[gvKCS.FocusedRowHandle]["SoChungTu"] + "'");
                if (message.Equals("success"))
                {
                    DataAll.Delete("KCS", "ID =" + dataview[gvKCS.FocusedRowHandle]["KCSID"]);
                }
                MessageBox.Show(message.Equals("success") ? "Gửi trả thành công" : message, "Message",
                                   MessageBoxButtons.OK,
                                   message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                LoadData();
            }
        }
    }
}
